package com.xingkeduo.business.order.remote;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.order.remote.feign.AdminFeignService;
import com.xingkeduo.dto.UserRemoteDto;
import com.xingkeduo.dto.WxMaUserInfo;
import com.xingkeduo.util.pojo.exception.BusinessException;
import com.xingkeduo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Optional;

/**
 * @author chi  2019-03-13 10:58
 **/

@Service
@Slf4j
public class UserRemoteService {

    @Resource
    private AdminRemoteService adminRemoteService;

    public Optional<UserRemoteDto> getByUserId(String userId){
        log.info("获取用户信息:id = {}", userId);

        if(StringUtils.isBlank(userId)){
            return Optional.empty();
        }

        ResponseUtils<WxMaUserInfo> consumerInfo = adminRemoteService.getConsumerInfo(userId);

        if(consumerInfo == null){
            log.info("获取用户返回空");
        }else {
            log.info("获取用户返回:{}", JSON.toJSONString(consumerInfo));
            if("9004".equals(consumerInfo.getCode())){
                return Optional.empty();
            }
        }

        if(consumerInfo != null && consumerInfo.success()){
            WxMaUserInfo data = consumerInfo.getData();
            UserRemoteDto userRemoteDto = new UserRemoteDto();
            BeanUtils.copyProperties(data, userRemoteDto);

            userRemoteDto.setUserHeadUrl(data.getAvatarUrl());
            userRemoteDto.setUserId(userId);
            userRemoteDto.setUserMobile(data.getPhone());
            userRemoteDto.setUserNickName(data.getNickName());

            return Optional.ofNullable(userRemoteDto);

        }else {
            return Optional.empty();
        }
    }



}
